package com.ewentsai;

import java.sql.*;

public class DB {
    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://localhost/ewentsai_tk?characterEncoding=utf-8";

//    private static final String USER = "ewentsai_tk";
//    private static final String PASS = "VincentMok2";

    private static final String USER = "root";
    private static final String PASS = "root";

    public static void main(String[] arg0){
        Connection connection = DB.getConnection();
        Statement statement = DB.createStatement(connection);
        ResultSet rSet = DB.executeQuery(statement,"select * from article");
        try {
            while (rSet.next()) {
                String title = rSet.getString("title");
                Date time = rSet.getDate("time");
                String author = rSet.getString("author");
                String content = rSet.getString("content");
//                Article article = new Article(title, time, author, content);
                System.out.println(title+time+author+content);
//                articles.add(article);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(DB_URL,USER,PASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static Statement createStatement(Connection connection){
        Statement statement = null;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return statement;
    }

    public static ResultSet executeQuery(Statement statement, String SQL){
        ResultSet rSet = null;
        try {
            rSet = statement.executeQuery(SQL);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rSet;
    }

    public static int executeUpdate(Statement statement, String SQl){
        int ret = 0;
        try {
            ret = statement.executeUpdate(SQl);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }
}